using System;
using System.Text;
using System.Collections.Generic;

using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Logging.Configuration;
using System.Collections.Specialized;


namespace SBPweb.Logging
{
    /// <summary>
    /// Formattes LogEntity instances as XML tags.
    /// </summary>
	[ConfigurationElementType(typeof(CustomFormatterData))]
	public class LogXmlFormatter : EntLibTextFormatter
	{
		private static string mTimestampFormat;

		/// <summary>
		/// Returns a string that represents the format of TimeStamps
		/// </summary>
		public static string TimestampFormat
		{
			get
			{
				return mTimestampFormat;
			}
			set
			{
				mTimestampFormat = value;
			}
		}
		
		/// <summary>
		/// Returns the TimestampToken
		/// </summary>
		protected static string TimestampToken
		{
			get
			{
				if (mTimestampFormat == null)
				{
					return "{timestamp}";
				}
				else
				{
					return "{timestamp(" + mTimestampFormat + ")}";
				}
			}
		}

		/// <summary>
		/// Called by <see cref="LogManager"></see> to initialize related objects.
		/// </summary>
		/// <remarks>
		/// By calling this the <see cref="LogManager"/> transforms this instance to be initialized and removes it from the collection
		/// of uninitialized formatters. The initialization procelss can be started by calling <see cref="LogManager.Initialize()"/>.
		/// This method supports the EntLib infrastructure.
		/// </remarks>
		protected internal override void InitializeRelatedObjects()
		{
			// Caching Formatter attributes
			try
			{
				this.Template = "<logentry timestamp=\"" + TimestampToken + "\" thread=\"{win32ThreadId}\" level=\"{severity}\" keywords=\"{keyword}\">{message}</logentry>";
			}
			catch { }
		}

		/// <summary>
		/// Parameterless constructor in order to support ObjectBuilder.
		/// </summary>
		public LogXmlFormatter()
			: base()
		{
		}

		/// <summary>
		/// This constructor supports ObjectBuilder.
		/// </summary>
		public LogXmlFormatter(NameValueCollection nvc)
			: base()
		{
		}
	}
}
